﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    public float m_speed = 1;
    Transform m_transform = null;
    public Transform m_rocket = null;
    float m_rocketTime = 0;
    public float m_life=50;
    public AudioClip m_shootClip;
    AudioSource m_audio;
    public Transform m_explosionFX;
    public LayerMask m_layer;
    Vector3 m_targetPos;
    Camera mainCamera;
	// Use this for initialization
	void Start () {
        m_transform = this.transform;
        m_audio = GetComponent<AudioSource>();
        m_targetPos = transform.position;
        mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
        
    }

    void moveTo()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 ms = Input.mousePosition;
            Ray ray = mainCamera.ScreenPointToRay(ms);
            RaycastHit raycastHit;
            bool isCast = Physics.Raycast(ray, out raycastHit, 1000, m_layer);
            if (isCast)
            {
                m_targetPos = raycastHit.point;
            }

            Vector3 pos = Vector3.MoveTowards(m_transform.position, m_targetPos, Time.deltaTime*m_speed);
            m_transform.position = pos;

        }
    }
    // Update is called once per frame
    void Update () {

        moveTo();
        m_rocketTime -= Time.deltaTime;
        if (m_rocketTime <= 0)
        {
            m_rocketTime = 0.1f;
            if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
            {
                Vector3 position = m_transform.position;
                position.Set(position.x, position.y, position.z + 0.5f);
                Instantiate(m_rocket, position, m_transform.rotation);
                m_audio.PlayOneShot(m_shootClip);
            }
        }

        float moveX = 0,moveY=0;

        if (Input.GetKey(KeyCode.UpArrow)|| Input.GetKey(KeyCode.W))
        {
            moveY += m_speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
        {
            moveY -= m_speed * Time.deltaTime;

        }

        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            moveX -= m_speed * Time.deltaTime;

        }

        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            moveX += m_speed * Time.deltaTime;

        }


       
       

        m_transform.Translate(new Vector3(moveX,0,moveY));


    }

    private void OnTriggerEnter(Collider other)
    {
          if (other.tag!="PlayRocket")
        {
            if (other.tag=="EnemyRocket")
            {
                m_life -= other.GetComponent<EnemyRocket>().m_power;
                Manager.Instance.changeLife((int)m_life);
            }
            
            if (m_life<=0)
            {
                Instantiate(m_explosionFX, transform.position, Quaternion.identity);
                Destroy(gameObject);
            }
        }
    }

}
